package com.lanou.beautifulwardrobe.activity;

import android.graphics.Paint;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.GridView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.ScrollView;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader;
import com.google.gson.Gson;
import com.lanou.beautifulwardrobe.Brand.EditViewPager;
import com.lanou.beautifulwardrobe.Home.Adapter.DetailGridViewAdapter;
import com.lanou.beautifulwardrobe.Home.Adapter.DetailViewPagerAdapter;
import com.lanou.beautifulwardrobe.Home.Adapter.ParameterListViewAdapter;
import com.lanou.beautifulwardrobe.Home.Adapter.ProductDetailAdapter;
import com.lanou.beautifulwardrobe.Home.Adapter.ProductParameterAdapter;
import com.lanou.beautifulwardrobe.Home.DetailFragment;
import com.lanou.beautifulwardrobe.Home.ParameterFragment;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.View.FullyGridLayoutManager;
import com.lanou.beautifulwardrobe.contants.Contants;
import com.lanou.beautifulwardrobe.contants.MyGridView;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.DetailItemImgs;
import com.lanou.beautifulwardrobe.entify.DetailMobileDesc;
import com.lanou.beautifulwardrobe.entify.DetailPropsName;
import com.lanou.beautifulwardrobe.entify.DetailResult;

import java.util.List;

/**
 * Created by dllo on 15/11/2.
 */
public class ProductDetailActivity extends FragmentActivity implements AdapterView.OnItemClickListener, View.OnClickListener {
    private TextView detailTitle, detailSellPrice, detailPrice, detailDiscount, detailVolunme;
    private ViewPager detailViewPager;
    private ImageButton productDetailBack;

    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String id;
    private DetailViewPagerAdapter adapter;

    private RadioGroup radiogroup;
    private RadioButton button1;
    private RadioButton button2;

    private RadioGroup action_radiogroup;
    private RadioButton action_button1;
    private RadioButton action_button2;


    private ListView detailLv;
    private ListView paraLv;
    private LinearLayout invis;
    private DetailGridViewAdapter viewAdapter;
    private ParameterListViewAdapter parameterListViewAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.hotlist_product_detail);
        id = getIntent().getStringExtra("id");
        initView();
        initData();
    }

    private void initView() {
        // detailGridView= (MyGridView) findViewById(R.id.detailGridView);
//        detailTitle = (TextView) findViewById(R.id.detailTitle);
//        detailSellPrice = (TextView) findViewById(R.id.detailSellPrice);
//        detailPrice = (TextView) findViewById(R.id.detailPrice);
//        detailDiscount = (TextView) findViewById(R.id.detailDiscount);
//        detailVolunme = (TextView) findViewById(R.id.detailVolunme);

        // detailViewPager = (ViewPager) findViewById(R.id.detailViewPager);
        adapter = new DetailViewPagerAdapter(this);

        radiogroup = (RadioGroup) findViewById(R.id.radiogroup);
        button1 = (RadioButton) findViewById(R.id.button1);
        button2 = (RadioButton) findViewById(R.id.button2);
        productDetailBack = (ImageButton) findViewById(R.id.productDetailBack);

        detailLv = (ListView) findViewById(R.id.detailListView);
        //  paraLv= (ListView) findViewById(R.id.parameterListView);
        invis = (LinearLayout) findViewById(R.id.invis);
        viewAdapter = new DetailGridViewAdapter();
        parameterListViewAdapter = new ParameterListViewAdapter();




    }

    private void initData() {
        View header = View.inflate(this, R.layout.listview_detail_header, null);//头部内容

        detailTitle = (TextView) header.findViewById(R.id.detailTitle);
        detailSellPrice = (TextView) header.findViewById(R.id.detailSellPrice);
        detailPrice = (TextView) header.findViewById(R.id.detailPrice);
        detailDiscount = (TextView) header.findViewById(R.id.detailDiscount);
        detailVolunme = (TextView) header.findViewById(R.id.detailVolunme);
        detailViewPager = (ViewPager) header.findViewById(R.id.detailViewPager);


        final View action = View.inflate(this, R.layout.listview_detail_action, null);

        action_button1 = (RadioButton) action.findViewById(R.id.action_button1);
        action_button2 = (RadioButton) action.findViewById(R.id.action_button2);
        action_radiogroup = (RadioGroup) action.findViewById(R.id.action_radiogroup);

        detailLv.addHeaderView(header);
        detailLv.addHeaderView(action);
        detailLv.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                if (firstVisibleItem >= 1) {
                    invis.setVisibility(View.VISIBLE);
                } else {

                    invis.setVisibility(View.GONE);
                }
            }
        });


        final String detailUrl = Contants.DETAIL_URL_PREFIX + id + Contants.DETAIL_URL_SUFFIX;
        Log.i("lanou", detailUrl + "///**");

        volleySingleton.getStringRequset(detailUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                final DetailResult detailResult = gson.fromJson(response, DetailResult.class);
                if (detailResult != null) {
                    List<DetailItemImgs> imgsList = detailResult.getData().getTaobao_item_imgs();
                    adapter.addData(imgsList);
                    detailViewPager.setAdapter(adapter);
                    detailTitle.setText(detailResult.getData().getTaobao_title());
                    detailSellPrice.setText(detailResult.getData().getMoney_symbol() + detailResult.getData().getTaobao_selling_price());
                    detailPrice.setText(detailResult.getData().getMoney_symbol() + detailResult.getData().getTaobao_price());
                    detailDiscount.setText(detailResult.getData().getDiscount());
                    detailVolunme.setText(detailResult.getData().getTaobao_volume());
                    detailPrice.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);


                    final List<DetailMobileDesc> detailMobileDescs = detailResult.getData().getMobile_desc();
                    viewAdapter.addData(detailMobileDescs);
                    detailLv.setAdapter(viewAdapter);

                    final List<DetailPropsName> detailPropsNames = detailResult.getData().getProps_name();
                    // paraLv.setAdapter(parameterListViewAdapter);
                    //  detailLv.setAdapter(parameterListViewAdapter);

                    radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId) {
                                case R.id.button1:
                                    Toast.makeText(ProductDetailActivity.this, "button1", Toast.LENGTH_SHORT).show();
                                    viewAdapter.addData(detailMobileDescs);
                                    //  paraLv.setVisibility(View.VISIBLE);
                                    action_button1.setClickable(false);
                                    action_button2.setClickable(true);
                                    button2.setClickable(true);
                                    break;
                                case R.id.button2:
                                    Toast.makeText(ProductDetailActivity.this, "button2", Toast.LENGTH_SHORT).show();
                                    viewAdapter.addParaData(detailPropsNames);
                                    //  detailLv.setVisibility(View.GONE);
                                    action_button2.setClickable(false);
                                    action_button1.setClickable(true);
                                    button1.setClickable(true);
                                    break;
                            }
                        }
                    });
                    action_radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(RadioGroup group, int checkedId) {
                            switch (checkedId) {
                                case R.id.action_button1:
                                    Toast.makeText(ProductDetailActivity.this, "action_button1", Toast.LENGTH_SHORT).show();
                                    viewAdapter.addData(detailMobileDescs);
                                    button1.setClickable(false);
                                    action_button2.setClickable(true);
                                    button2.setClickable(true);
                                    break;
                                case R.id.action_button2:
                                    Toast.makeText(ProductDetailActivity.this, "action_button2", Toast.LENGTH_SHORT).show();
                                    viewAdapter.addParaData(detailPropsNames);
                                    button2.setClickable(false);
                                    button1.setClickable(true);
                                    action_button1.setClickable(true);
                                    break;
                            }
                        }
                    });
                }
            }
        });

        detailLv.setOnItemClickListener(this);
        productDetailBack.setOnClickListener(this);
    }


    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(ProductDetailActivity.this, position + "*", Toast.LENGTH_SHORT).show();
    }


    public void onClick(View v) {
        finish();
    }
}
